// pages/bindphone/bindphone.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    red:0,
    txt:"获取验证码",
    time: 60,
    phone: "",
    code: "",
    password:""

  },
  getphone:function(){

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
  },
  // 绑定
  bangding:function(){
    var obj = { MobilePhone: this.data.phone, Code: this.data.code, PassWord: this.data.password}
    var query=[];
    query.push(obj)
    ajax('/api/UpdatePhone/Update?requestContent=' + JSON.stringify({
      query:query,
      token: wx.getStorageSync('Access_token'),

    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "POST",
      }, res => {
        console.log(res)
        if (res.Message == "成功!"){
          wx.showToast({
            title: res.Message,
            icon: 'none',
            duration: 2000
          })
          wx.reLaunch({
            url: '/pages/mine/mine',
          })
        }else{
          wx.showToast({
            title: "绑定失败",
            icon: 'none',
            duration: 2000
          })
        }
        
      }
    )
  },
  getphonecode: function () {
    this.setData({
      red: 1
    });
    if (this.data.time == 60) {
      if (this.checkPhone()) {
        ajax('/api/SendTemplateSMS/Get?requestContent=' + JSON.stringify({
          CardID: 3,
          Vtype: 6,
          MobilePhone: this.data.phone

        }), {
            header: {
              'Accept': 'application/x-www-form-urlencoded', // 默认值
            },
            method: "get",
          }, res => {
            console.log(res)
            if (res.Status == 1){
              wx.showToast({
                title: '发送成功',
                icon: 'none',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: '该号码已经被注册过！',
                icon: 'none',
                duration: 2000
              })
            }
            
          }
        )

        console.log("1")
      } else {
        wx.showToast({
          title: '请输入正确手机号',
          icon: 'none',
          duration: 2000
        })
        return false;
      }

      var that = this
      var timer = setInterval(function () {
        that.data.time--
        that.setData({
          txt: that.data.time + "秒后重新获取"
        })
        if (that.data.time == 0) {
          that.setData({
            txt: "重新发送",
            time: 60
          })
          clearInterval(timer)
        }
      }, 1000)
    } else {
      return false
    }
    
  },
  isPhoneNumber: function (tel) {
    var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
    return reg.test(tel);
  },
  // 获取手机号
  getphone: function (e) {
    // console.log(1)
    this.setData({
      phone: e.detail.value
    });
  },
  // 获取验证码
  getcode: function (e) {
    this.setData({
      code: e.detail.value
    });
    
    
  },
  // 获取密码
  getpassword: function (e) {
    this.setData({
      password: e.detail.value
    });
  },

  checkPhone: function () {
    var phone = this.data.phone
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
      return false;
    } else {
      return true;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})